//
//  ZSStatusRetweetedView.swift
//  ZSWeibo
//
//  Created by zsj1992 on 16/4/13.
//  Copyright © 2016年 ichange. All rights reserved.
//

import UIKit
import SnapKit

//转发视图
class ZSStatusRetweetedView: UIView {
    
    
    
    var bottomCons:Constraint?
    
    var statusViewModel:ZSStatusViewModel?{
        
        didSet{
        
            
            
            contentLabel.attributedText = statusViewModel?.retwetedAttributeString
         
            //配图视图设置数据源
            self.bottomCons?.uninstall()
            if let urls = statusViewModel?.status?.retweeted_status!.pic_urls where urls.count > 0 {
                //有配图视图
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(ZSStatusCellMargin).constraint
                })
                //设置数据源
                pictureView.imageURLs = urls
                //显示配图视图
                pictureView.hidden = false
                
            } else {
                //没有配图视图
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomCons = make.bottom.equalTo(contentLabel.snp_bottom).offset(ZSStatusCellMargin).constraint
                })
                //设置数据源 nil
                pictureView.imageURLs = nil
                //隐藏配图视图
                pictureView.hidden = true
                
            }

            
        
        }
    
    }
    
    
    
    //重写构造方法,一些抽出去的方法在这里进行实现
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        contentLabel.font = UIFont.systemFontOfSize(14)
        contentLabel.numberOfLines = 0
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    
    
    
    
    
    
    //懒加载子视图
    lazy var contentLabel : UILabel = UILabel()
    lazy var pictureView : ZSStatusPictureView = ZSStatusPictureView()
}


// MARK: - 这里是转发微博视图的扩展方法

extension ZSStatusRetweetedView{
    
    /**设置约束等*/
    func setupUI(){
        addSubview(contentLabel)
        addSubview(pictureView)

    contentLabel.backgroundColor = UIColor(red: 222/255.0, green: 222/255.0, blue: 222/255.0, alpha: 1.0)

    contentLabel.preferredMaxLayoutWidth = screenWidth - CGFloat(2) * CGFloat( ZSStatusCellMargin)
    
    //设置文字的约束
    contentLabel.snp_makeConstraints { (make) -> Void in
        
    make.left.equalTo(self).offset(ZSStatusCellMargin)
        
    make.top.equalTo(self).offset(ZSStatusCellMargin)

        }
        
        
        
    self.snp_makeConstraints { (make) -> Void in
        
      self.bottomCons =  make.bottom.equalTo(pictureView.snp_bottom).constraint
        
        }
    
        
        
    //设置转发视图配图的约束
    pictureView.snp_makeConstraints { (make) -> Void in
        
          make.top.equalTo(contentLabel.snp_bottom).offset(ZSStatusCellMargin)
        make.left.equalTo(self).offset(ZSStatusCellMargin)
        
        }
    
        
        
        
    }
    

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}





